package live.ifox.stage;

import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import live.ifox.base.BaseRestActivity;
import live.ifox.factory.Entry;
import live.ifox.factory.ID;
import live.ifox.factory.StaffInfo;
import live.ifox.factory.Url;
import live.ifox.util.CommonUtil;

public class AccountEditor extends BaseRestActivity {
    boolean updateFlag;
    List<String> groups;
    List<String> levels;
    EditText user_name, user_pass;
    Spinner user_group, user_level;
    int _id, _level;
    String _name, _group;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.account_editor);
        actionBar.setTitle(R.string.account);

        entry = Entry.Account;
        baseUrl = Url.Account;
        user_name = findViewById(R.id.user_name);
        user_pass = findViewById(R.id.user_pass);
        user_group = findViewById(R.id.user_group);
        user_level = findViewById(R.id.user_level);
        initializeGroupSpinner();
        initializeLevelSpinner();

        _id = getIntent().getIntExtra("id", 0);
        if (_id > 0) {
            updateFlag = true;
            _name = getIntent().getStringExtra("user_name");
            _group = getIntent().getStringExtra("user_group");
            _level = getIntent().getIntExtra("user_level", 0);

            user_name.setText(_name);
            if (groups.contains(_group))
                user_group.setSelection(groups.indexOf(_group));

            String level = StaffInfo.getLevelString(_level);
            if (levels.contains(level)) {
                user_level.setSelection(levels.indexOf(level));
            }
        }
    }

    void initializeGroupSpinner() {
        groups = new ArrayList<>();
        groups.add(StaffInfo.Group.None);
        groups.add(StaffInfo.Group.TD);
        groups.add(StaffInfo.Group.SD);
        groups.add(StaffInfo.Group.GD);
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
                android.R.layout.simple_spinner_dropdown_item);
        adapter.addAll(groups);
        user_group.setAdapter(adapter);
    }

    void initializeLevelSpinner() {
        levels = new ArrayList<>();
        levels.add(StaffInfo.getLevelString(StaffInfo.Level.User));
        levels.add(StaffInfo.getLevelString(StaffInfo.Level.Manager));
        levels.add(StaffInfo.getLevelString(StaffInfo.Level.Developer));
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
                android.R.layout.simple_spinner_dropdown_item);
        adapter.addAll(levels);
        user_level.setAdapter(adapter);
    }

    @Override
    public void processReply(JSONObject rootObject) {

    }

    public void onClean_Click(View view) {
        if (!user_name.getText().toString().isEmpty())
            user_name.setText("");

        if (!user_pass.getText().toString().isEmpty())
            user_pass.setText("");
    }

    public void onSubmit_Click(View view) throws java.security.NoSuchAlgorithmException {
        String name = user_name.getText().toString();
        if (name.isEmpty())
            return;

        String pass = user_pass.getText().toString();
        int level = StaffInfo.getLevel(user_level.getSelectedItem().toString());
        String group = user_group.getSelectedItem().toString();
        Map<String, Object> map = new HashMap<>();
        if (!updateFlag) {
            if (pass.isEmpty())
                return;

            map.put("user_name", name);
            map.put("user_pass", CommonUtil.encrypt("MD5", pass));
            map.put("user_level", level);
            map.put("user_group", group);
            httpPost(baseUrl + "insert", JSON.toJSONString(map));
        } else {
            if (!_name.equals(name))
                map.put("user_name", name);

            if (!pass.isEmpty())
                map.put("user_pass", CommonUtil.encrypt("MD5", pass));

            if (_level != level)
                map.put("user_level", level);

            if (!_group.equals(group))
                map.put("user_group", group);

            if (map.isEmpty())
                return;

            map.put("id", _id);
            httpPost(baseUrl + "update", JSON.toJSONString(map));
        }
        setResult(ID.RESULT_DONE);
        finish();
    }
}